home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 440_01 / !best003.c < prev    next >
C/C++ Source or Header  |  1994-04-12  |  18KB  |  460 lines

  1. /*==========================================================================
  2.  *
  3.  *  !BEST003.C                                       Tuesday, April 12, 1994
  4.  *
  5.  *  640x480x16 graphics mode windows routines
  6.  *  supplementary source file 3 for The BESTLibrary
  7.  *
  8.  *  Authored independently by George Vanous
  9.  *
  10.  *==========================================================================*/
  11.  
  12.  
  13. /* ------------------------------------------------------------------------ */
  14. /* ----------------------------  INCLUDE FILES  --------------------------- */
  15.  
  16. #include <alloc.h>
  17. #include <stdlib.h>
  18. #include <graphics.h>
  19. #include "!bestlib.h"                  // include !BESTLIB.H in compilation
  20.  
  21. /* ------------------------------------------------------------------------ */
  22. /* -------------------------  GLOBAL DEFINITIONS  ------------------------- */
  23.  
  24. extern mousedata msdata;
  25. extern asciiscan key;
  26. extern char *textstring;
  27.  
  28. /* ------------------------------------------------------------------------ */
  29.  
  30.  
  31. /*----------------------------------------------------------------------------
  32.  * Pop up a column in 640x480x16 graphics mode.
  33.  *
  34.  * [see !BESTLIB.DOC for a description of each parameter]
  35.  * [see !BESTLIB.DOC for a description of the return values]
  36.  */
  37. int _16_popupcolumn(int x, int y, int choice, int maxmenus, char *ptr[], int ms)
  38. {
  39.   #define MAXDATA 20                      // maximum number of menu options
  40.   #define BORDERTHICKNESS 3            // thickness of border in pixels
  41.   #define BORDERCOLOR YELLOW           // color of surrounding border
  42.   #define INNERXSPACE 3                // space between left border and text
  43.   #define INNERYSPACE 2                // space between top border and text
  44.   #define INTERSPACE 33                // space between horizontal submenus
  45.   #define INNERCOLOR BLUE              // inner color of menu
  46.   #define FILLSTYLE 1                  // fill pattern of inner menu
  47.   #define TEXTCOLOR YELLOW             // color of text
  48.   #define INACTIVECOLOR LIGHTRED       // color of inactive text
  49.   #define BARCOLOR WHITE               // color of choosebar
  50.   int barx, bary,
  51.       barlength,
  52.       flength, fheight,
  53.       textx, texty,
  54.       height = 0, length = 0;
  55.   imagedata *menubg;
  56.   register int i, ii;
  57.  
  58.   choice--;
  59.   for (i = 0; i < maxmenus; i++) {
  60.     if (textwidth(ptr[i]) > length) length = textwidth(ptr[i]);
  61.     if (textheight(ptr[i]) > height) height = textheight(ptr[i]);
  62.   }
  63.   height++;
  64.   fheight = height * maxmenus + BORDERTHICKNESS * 2 + INNERYSPACE * 2,
  65.   flength = length + BORDERTHICKNESS * 2 + INNERXSPACE * 2;
  66.   if ((menubg = (void *) malloc(_16_i_need(flength, fheight))) == NULL)
  67.     return(-1);                        // return code for error
  68.   _16_i_save(x, y, flength, fheight, menubg, FALSE, TRUE);
  69.   for (i = 0; i < BORDERTHICKNESS; i++)
  70.     _16_boxoutline(x + i, y + i, flength - i - i, fheight - i - i, BORDERCOLOR, COPY_IMAGE);
  71.  
  72.   _16_boxfill(x + i, y + i, flength - i - i, fheight - i - i, BLACK, COPY_IMAGE);
  73.   textx = x + BORDERTHICKNESS + INNERXSPACE,
  74.   texty = y + BORDERTHICKNESS + INNERYSPACE;
  75.   setfillstyle(FILLSTYLE, INNERCOLOR);
  76.   floodfill(textx, texty, BORDERCOLOR);
  77.   setcolor(TEXTCOLOR);
  78.   for (ii = texty - 1, i = 0; i < maxmenus; ii += height, i++)
  79.     outtextxy(textx, ii, ptr[i]);
  80.   barlength = length + INNERXSPACE * 2,
  81.   fheight -= BORDERTHICKNESS,
  82.   barx = textx - INNERXSPACE,
  83.   bary = texty + choice * height;
  84.  
  85.   if (ms) {
  86.     msdata.pos[0] = msdata.npos[0] = MAXX / 2,
  87.     msdata.pos[1] = msdata.npos[1] = MAXY / 2;
  88.     ms_set(msdata.pos[0], msdata.pos[1]);
  89.   }
  90.  while (TRUE)
  91.  {
  92.    _16_boxfill(barx, bary, barlength, height, BARCOLOR, XOR_IMAGE);
  93.    key.ascii = key.scan = 0;
  94.    if (ms) while (TRUE) {
  95.      if (keyhit()) {
  96.        getchr();
  97.        break;
  98.      }
  99.      ms_stat();
  100.      if (msdata.buts[0] != msdata.nbuts[0]) {
  101.        msdata.buts[0] = msdata.nbuts[0];
  102.        if (!msdata.nbuts[0]) { key.ascii = 13; break; }
  103.      }
  104.      if (msdata.buts[1] != msdata.nbuts[1]) {
  105.        msdata.buts[1] = msdata.nbuts[1];
  106.        if (!msdata.nbuts[1]) { key.ascii = 27; break; }
  107.      }
  108.      if (abs(length = msdata.npos[0] - msdata.pos[0]) > 40) {
  109.        if (length > 0) key.scan = 'M';
  110.        else key.scan = 'K';
  111.        msdata.pos[0] = msdata.npos[0] = MAXX / 2,
  112.        msdata.pos[1] = msdata.npos[1] = MAXY / 2;
  113.        ms_set(msdata.pos[0], msdata.pos[1]);
  114.        break;
  115.      }
  116.      if (abs(length = msdata.npos[1] - msdata.pos[1]) > 10) {
  117.        if (length > 0) key.scan = 'P';
  118.        else key.scan = 'H';
  119.        msdata.pos[0] = msdata.npos[0] = MAXX / 2,
  120.        msdata.pos[1] = msdata.npos[1] = MAXY / 2;
  121.        ms_set(msdata.pos[0], msdata.pos[1]);
  122.        break;
  123.      }
  124.    }
  125.    else
  126.      getchr();
  127.    _16_boxfill(barx, bary, barlength, height, BARCOLOR, XOR_IMAGE);
  128.    if (key.ascii == 9 || key.ascii == 13 || key.ascii == 27) {
  129.      if (key.ascii == 9 || key.ascii == 27) choice = -1;
  130.      break;
  131.    }                                   // 'H' = 1, 'P' = 4
  132.    if (key.scan == 'H') {
  133.      if (--choice < 0) choice = maxmenus - 1, bary = texty + height * (maxmenus - 1);
  134.      else bary -= height;
  135.    }
  136.    else if (key.scan == 'P') {
  137.      if (++choice == maxmenus) choice = 0, bary = texty;
  138.      else bary += height;
  139.    }
  140.  }
  141.   _16_i_show(x, y, menubg, COPY_IMAGE);
  142.   free(menubg);
  143.   return(++choice);
  144. }
  145.  
  146. /*----------------------------------------------------------------------------
  147.  * Pop up a menu in 640x480x16 graphics mode.
  148.  *
  149.  * [see !BESTLIB.DOC for a description of each parameter]
  150.  * [see !BESTLIB.DOC for a description of the return values]
  151.  */
  152. int _16_popupmenu(int x, int y, int choice, int maxmenus, char *ptr[], int data[][3], char *text[], int ms)
  153. {
  154.   #define MAXDATA 20                  // maximum number of menu options
  155.   #define BORDERTHICKNESS 3           // thickness of border in pixels
  156.   #define BORDERCOLOR YELLOW          // color of surrounding border
  157.   #define INNERXSPACE 3               // space between left border and text
  158.   #define INNERYSPACE 2               // space between top border and text
  159.   #define INTERSPACE 33               // space between horizontal submenus
  160.   #define INNERCOLOR BLUE             // inner color of menu
  161.   #define FILLSTYLE 1                 // fill pattern of inner menu
  162.   #define TEXTCOLOR YELLOW            // color of text
  163.   #define INACTIVECOLOR LIGHTRED      // color of inactive text
  164.   #define BARCOLOR WHITE              // color of choosebar
  165.   int datalength[MAXDATA] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  166.   int a, b, bgx, bgy, bx, barx, bary, barlength, choicex = 0,
  167.       length, flength, height, fheight, textx, texty;
  168.   imagedata *bg, *bg2, *menubg;
  169.   register int i, ii;
  170.  
  171.   choice--;
  172.   for (a = i = 0; i < maxmenus; i++) {
  173.     for (ii = 0; ii < data[i][0]; ii++) {
  174.       if (!data[i][1])
  175.         datalength[i] = 0;
  176.       else
  177.         datalength[i] += textwidth( text[ data[i][1] + ii] ) + INTERSPACE;
  178.     }
  179.     if (datalength[i] > a)
  180.       a = datalength[i];
  181.   }
  182.   for (b = i = ii = 0; i < maxmenus; i++) {
  183.     if (textwidth(ptr[i]) > ii)
  184.       ii = length = textwidth(ptr[i]);
  185.     if (textheight(ptr[i]) > b)
  186.       b = height = textheight(ptr[i]);
  187.   }
  188.   height++;
  189.   bg = (void *) malloc(_16_i_need(a + BORDERTHICKNESS * 2, height + BORDERTHICKNESS * 2 + INNERYSPACE * 2));
  190.   bg2 = (void *) malloc(_16_i_need(BORDERTHICKNESS, height + BORDERTHICKNESS * 2 + INNERYSPACE * 2));
  191.   fheight = height * maxmenus + BORDERTHICKNESS * 2 + INNERYSPACE * 2,
  192.   flength = length + BORDERTHICKNESS * 2 + INNERXSPACE * 2;
  193.   menubg = (void *) malloc(_16_i_need(flength, fheight));
  194.   if (menubg == NULL || bg2 == NULL || bg == NULL) {
  195.     if (bg != NULL) free(bg);
  196.     if (bg2 != NULL) free(bg2);
  197.     if (menubg != NULL) free(menubg);
  198.     return(-1);                        // return code for error
  199.   }
  200.   _16_i_save(x, y, flength, fheight, menubg, FALSE, TRUE);
  201.   for (i = 0; i < BORDERTHICKNESS; i++)
  202.     _16_boxoutline(x + i, y + i, flength - i - i, fheight - i - i, BORDERCOLOR, COPY_IMAGE);
  203.  
  204.   _16_boxfill(x + i, y + i, flength - i